package com.woniuxy.meal.util;

import org.springframework.data.redis.core.StringRedisTemplate;
import org.springframework.data.redis.core.ZSetOperations;
import org.springframework.stereotype.Component;

import javax.annotation.Resource;
import java.util.HashSet;
import java.util.List;
import java.util.Map;
import java.util.Set;
import java.util.concurrent.TimeUnit;

@Component
public class RedisUtil {
    @Resource
    private StringRedisTemplate stringRedisTemplate;

    /**
     * 读取数据
     */
    public String getString(final String key) {
        return stringRedisTemplate.opsForValue().get(key);
    }

    /**
     * 写入数据并设置有效时间
     */
    public void setString(final String key, String value,Integer second) {
        stringRedisTemplate.opsForValue().set(key, value,second,TimeUnit.SECONDS);
    }
    /**
     * 写入数据
     */
    public void setString(final String key, String value) {
        stringRedisTemplate.opsForValue().set(key, value);
    }
    /**
     * 删除数据
     */
    public void delete(final String key) {
        stringRedisTemplate.delete(key);
    }

    /**
     * 键值自增
     */
    public void  incr(final String key){
        stringRedisTemplate.opsForValue().increment(key);
    }

    /**
     * 判断健是否存在
     */
    public Boolean exist(String key){
        Boolean bool = stringRedisTemplate.hasKey(key);
        return bool;
    }

    /**
     * 添加set数据
     */
    public void sadd(String key,String value){
        stringRedisTemplate.opsForSet().add(key,value);
    }

    /**
     * 判断set中是否存在某个值
     */

    public boolean sisMember(String key,String value){
        return stringRedisTemplate.opsForSet().isMember(key, value);
    }

    /**
     * 删除set中的某一个值
     */
    public void sremove(String key,String value){
        stringRedisTemplate.opsForSet().remove(key,value);
    }

    /**
     * 查询set数据
     */
    public Set<String> smembers(String key) throws Exception{
        return stringRedisTemplate.opsForSet().members(key);
    }


    //解析token获取userId
    public Long getUserIdByToken(String token){
        String longToken = getString(token);
        Object id = JwtUtil.parseToken(longToken).get("sub");
        return  Long.parseLong(id+"");
    }

    /**
     * 向zset中添加数据
     */
    public void zadd(String key,String value,double score) throws Exception{
        stringRedisTemplate.opsForZSet().add(key,value,score);
    }

    /**
     * zset排序值自增方法
     */
    public void zincrby(String key,String value,double number) throws Exception{
        stringRedisTemplate.opsForZSet().incrementScore(key, value, number);
    }

    /**
     * 删除zset中某一个值的方法
     */
    public void zremove(String key,String value) throws Exception{
        stringRedisTemplate.opsForZSet().remove(key, value);
    }

    /**
     * 查询zset排序值
     */
    public double zscore(String key,String value) throws Exception{
        return stringRedisTemplate.opsForZSet().score(key, value);
    }

    /**
     * 获取zset中的所有数据
     */
    public Set<String> zrange(String key) throws Exception{
        return stringRedisTemplate.opsForZSet().range(key, 0, -1);
    }

    /**
     * 获取zset中的所有数据带分数
     */
    public Set<ZSetOperations.TypedTuple<String>> zrangeWithScore(String key) throws Exception{
        return stringRedisTemplate.opsForZSet().rangeWithScores(key,0, -1);
    }

    /**
     * 按照排序值从大到小查询某一段数据
     */
    public Set<String> zReverseRange(String key,Integer start,Integer end) throws Exception{
        return stringRedisTemplate.opsForZSet().reverseRange(key, start, end);
    }

    public void hsPut(String key,String hashKey,Object value){
        stringRedisTemplate.opsForHash().put(key,hashKey,value);
    }

    public Map<Object,Object> hsGetAll(String key){
        return stringRedisTemplate.opsForHash().entries(key);
    }

    public void hsIncryby(String key,String hashKey,Integer count){
        stringRedisTemplate.opsForHash().increment(key,hashKey,count);
    }

    public boolean hsHasKey(String key, String hashKey) throws Exception{
        return stringRedisTemplate.opsForHash().hasKey(key,hashKey);
    }

    public void hsPutAll(String key,Map map) throws Exception{
        stringRedisTemplate.opsForHash().putAll(key,map);
    }

    public Object hsGet(String key, String hashKey) throws Exception{
       return stringRedisTemplate.opsForHash().get(key,hashKey);
    }

    public Set<Object> hsGetKeys(String key) throws Exception{
        return stringRedisTemplate.opsForHash().keys(key);
    }

    public List<Object> hsGetValues(String key) throws Exception{
        return stringRedisTemplate.opsForHash().values(key);
    }

    public void  hsDel(String key,String hashKey) throws Exception{
        stringRedisTemplate.opsForHash().delete(key,hashKey);
    }

    public Set<Integer> hsGetIntegerKeys(String key) throws Exception{
        Set<Object> keys = stringRedisTemplate.opsForHash().keys(key);
        Set<Integer> set = new HashSet<>();
        for (Object o : keys) {
            set.add(Integer.parseInt(o.toString()));
        }
        return set;
    }






}
